package team.group.exam.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

import team.group.exam.biz.IPaperBiz;
import team.group.exam.entity.PaperCondition;
import team.group.exam.entity.TbPaper;
import team.group.exam.entity.TbPaperBank;
import team.group.exam.entity.TbQuestionbank;
import team.group.exam.entity.TbUser;

public class PaperAction implements Action{
    private IPaperBiz paperBiz;
    private int  singleSize;
    private List list;
    private TbPaper paper;
    private List<TbPaperBank> paperList;
    private List<TbPaper> paperNameList;
    private int paper_id;
    private PaperCondition paperCondition;
    private List<TbPaper> somePaperList;
    private List<TbPaperBank> paperAllList;
    private int thepoint;
    private int uid;
    private List<String> paperTypeList;
    
    public int getThepoint() {
    	return thepoint;
	}

	public void setThepoint(int thepoint) {
		this.thepoint = thepoint;
	}
	/**
     * 进行分页显示
     * @return
     */
    private int currentPage;
    private int totalPage;
    /**
     * 考生分数及错题集合
     * @return
     */
    List<TbQuestionbank> errList=new ArrayList();
 
 	public IPaperBiz getPaperBiz() {
		return paperBiz;
	}

	public void setPaperBiz(IPaperBiz paperBiz) {
		this.paperBiz = paperBiz;
	}
	
    public int getSingleSize() {
		return singleSize;
	}

	public void setSingleSize(int singleSize) {
		this.singleSize = singleSize;
	}
    
    public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}
	

    public TbPaper getPaper() {
		return paper;
	}

	public void setPaper(TbPaper paper) {
		this.paper = paper;
	}
    
	public List<TbPaperBank> getPaperList() {
		return paperList;
	}

	public void setPaperList(List<TbPaperBank> paperList) {
		this.paperList = paperList;
	}
    
	public List<TbPaper> getPaperNameList() {
		return paperNameList;
	}

	public void setPaperNameList(List<TbPaper> paperNameList) {
		this.paperNameList = paperNameList;
	}
	public int getPaper_id() {
		return paper_id;
	}
    
	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	

	public void setPaper_id(int paper_id) {
		this.paper_id = paper_id;
	}
	
	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public List<TbPaper> getSomePaperList() {
		return somePaperList;
	}

	public void setSomePaperList(List<TbPaper> somePaperList) {
		this.somePaperList = somePaperList;
	}

	public PaperCondition getPaperCondition() {
		return paperCondition;
	}

	public void setPaperCondition(PaperCondition paperCondition) {
		this.paperCondition = paperCondition;
	}
    public List<TbPaperBank> getPaperAllList() {
		return paperAllList;
	}

	public void setPaperAllList(List<TbPaperBank> paperAllList) {
		this.paperAllList = paperAllList;
	}
    
	public List<TbQuestionbank> getErrList() {
		return errList;
	}

	public void setErrList(List<TbQuestionbank> errList) {
		this.errList = errList;
	}

	/**
	 * 获取所有的试卷类型
	 */
	public String getAllPaperType(){
		paperTypeList = paperBiz.getPaperType();
		return SUCCESS;
	}

	/**
    * 自动组卷1222
    */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		paperBiz.addPaper(paper);
		list=paperBiz.addRandomPaper(10,paper);
		System.out.println("试卷题号："+list);
		return SUCCESS;
	}
	
	/**
	 *得到试卷题目
	 */
	public String getAllTitle(){
		paper=paperBiz.getById(paper_id);
		int pageSize=5;
		totalPage=paperBiz.getTotalPage(paper,pageSize);
		
		 if(currentPage<=0)
	 		{
			 currentPage=1;
	 		}
	     if(currentPage>=totalPage){
	 			
		     currentPage=totalPage;
	 		}
		paperList=paperBiz.getTbPaperBankList(paper,currentPage,pageSize);
		System.out.println("试题:"+paperList.size());
		return SUCCESS;	
	}
	
	/**
	 * 条件查询得到试卷列表
	 */
    public String getPaperNames(){
    	  paperNameList=paperBiz.getPaperName(paperCondition);
    	  return SUCCESS;
    }
    
 
	/**
    * 得到不分页的试卷题目列表
    */
    public String getAllTitleNopage(){
    	paper=paperBiz.getById(paper_id);
    	Map<String, Object> session=ActionContext.getContext().getSession();
		System.out.println("测试paper:"+paper);
	    session.put("paper", paper);
    	paperAllList=paperBiz.getAllTitleNopage(paper);
    	System.out.println("页面大小："+paperAllList.size());
		return SUCCESS;
    }
    
    /**
     * 得到用户的分数以及错题
     */
   
   public String getErrorAndScore(){
	   HttpServletRequest request = ServletActionContext.getRequest();
//	    thepoint=paperBiz.getScore(paper,request);    
	    thepoint=paperBiz.getScore(paper,request,1);
		errList=paperBiz.getErrorQuestSet(paper, request);

		Map<String,Object> session=ActionContext.getContext().getSession();
		
		TbPaper paper1=(TbPaper) session.get("paper");
		
		TbUser user = (TbUser)session.get("backuser");
		//user.setUserId(uid);
		System.out.println(user.getUserId());	
		paper1.setTbUser(user);
		paper1.setPaperRealGrade((double) thepoint);
	    paperBiz.addScoreAndUserId(paper1);
		System.out.println("thepoint is:"+this.getThepoint());
		return SUCCESS;
   }
    
	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public List<String> getPaperTypeList() {
		return paperTypeList;
	}

	public void setPaperTypeList(List<String> paperTypeList) {
		this.paperTypeList = paperTypeList;
	}
   
   
}


